import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/12 10:25
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        int[] mapping =  {8,9,4,0,2,1,3,5,7,6}, nums = {991,338,38};
        Solution solution = new Solution();
        solution.sortJumbled(mapping, nums);
    }
    Map<Integer, Integer> map;
    Map<Integer, Integer> sort;
    public int[] sortJumbled(int[] mapping, int[] nums) {
        map = new HashMap<>();
        sort = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put(i, mapping[i]);
        }
        int id = 0, n = nums.length;
        int[][] sortArr = new int[n][2];
        for (int num : nums) {
            sortArr[id][0] = id;
            sortArr[id][1] = num;
            sort.put(id++, tran(num));
        }
        Arrays.sort(sortArr, (a, b) -> {
            int v1 = sort.get(a[0]), v2 = sort.get(b[0]);
            if (v1 == v2) {
                // 转换之后数字大小相同
                return a[0] - b[0];
            }
            // 按照映射值递增排序
            return v1 - v2;
        });
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            ans[i] = sortArr[i][1];
        }
        return ans;
    }
    public int tran(int n) {
        if (n == 0) {
            return map.get(0);
        }
        int res = 0;
        int bit = 1;
        while (n > 0) {
            res += map.get(n % 10) * bit;
            bit *= 10;
            n /= 10;
        }
        return res;
    }
}
